package com.simon.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simon.train.business.domain.DailyTrain;
import com.simon.train.business.domain.DailyTrainExample;
import com.simon.train.business.domain.Train;
import com.simon.train.business.domain.TrainExample;
import com.simon.train.business.dto.TrainQueryDto;
import com.simon.train.business.dto.TrainSaveDto;
import com.simon.train.business.mapper.DailyTrainMapper;
import com.simon.train.business.mapper.TrainMapper;
import com.simon.train.business.vo.DailyTrainQueryVO;
import com.simon.train.business.vo.TrainQueryVO;
import com.simon.train.common.exception.BusinessException;
import com.simon.train.common.exception.BusinessExceptionEnum;
import com.simon.train.common.util.SnowUtil;
import com.simon.train.common.vo.PageVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainService {

    private static final Logger LOG = LoggerFactory.getLogger(TrainService.class);

    @Resource
    private TrainMapper trainMapper;

    @Resource
    private DailyTrainMapper dailyTrainMapper;

    public void SaveTrain(TrainSaveDto trainSaveDto){
        Train train = BeanUtil.copyProperties(trainSaveDto, Train.class);
        if (ObjectUtil.isNull(train.getId())){
            //对于首次保存 也就是新增 需要对字段单独做处理
            Train stationDB = SelectByUnique(trainSaveDto.getCode());

            if(ObjectUtil.isNotEmpty(stationDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }

            train.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            train.setCreateTime(time);
            train.setUpdateTime(time);
            trainMapper.insert(train);
        } else {
            train.setUpdateTime(DateTime.now());
            trainMapper.updateByPrimaryKey(train);
        }

    }

    /**
     * 将这个通过查询唯一键是否为空的方法给提取出来
     * 便于后续针对所有的唯一键的查询进行调用
     * @param
     * @return
     */
    private Train SelectByUnique(String code) {
        TrainExample trainExample = new TrainExample();
        trainExample.createCriteria().andCodeEqualTo(code);
        List<Train> list = trainMapper.selectByExample(trainExample);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else return null;
    }

    public PageVO<TrainQueryVO> QueryTrainList(TrainQueryDto trainQueryDto){
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("id desc");
        TrainExample.Criteria criteria = trainExample.createCriteria();
        LOG.info("当前请求的页码：{}", trainQueryDto.getPage());
        LOG.info("每页条数：{}", trainQueryDto.getPageSize());

        PageHelper.startPage(trainQueryDto.getPage(),trainQueryDto.getPageSize());
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        PageInfo<Train> pageInfo = new PageInfo<>(trainList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainQueryVO> lists = BeanUtil.copyToList(trainList, TrainQueryVO.class);
        PageVO<TrainQueryVO> pageVO = new PageVO<>();
        pageVO.setList(lists);
        pageVO.setTotal(pageInfo.getTotal());

        return pageVO;
    }

    /**
     * 查询全部的列车信息 根据code进行升序排列
     * @return
     */
    public List<TrainQueryVO> QueryTrainAll(){
        List<Train> trainList = selectAll();
        return BeanUtil.copyToList(trainList, TrainQueryVO.class);
    }

    public List<DailyTrainQueryVO> QueryTrainDailyAll(){
        List<DailyTrain> trainList = selectDailyAll();
        return BeanUtil.copyToList(trainList, DailyTrainQueryVO.class);
    }

    /**
     * 将这个部分单独提取出来变成一个方法
     * 方便后续不同服务之间的调用
     * @return
     */
    public List<Train> selectAll() {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        return trainMapper.selectByExample(trainExample);
    }

    public List<DailyTrain> selectDailyAll() {
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.setOrderByClause("code asc");
        return dailyTrainMapper.selectByExample(dailyTrainExample);
    }

    public void deleteTrain(Long id) {
        trainMapper.deleteByPrimaryKey(id);
    }
}
